﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 音频采样 
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class AudioVisable : MonoBehaviour
{
    private AudioSource audioSource;
    public float[] SampleArr = new float[512];
    private float[] freqBand = new float[8];
    private float[] bandBuffer = new float[8];
    private float[] bufferDecrease = new float[8];

    private float[] freqBandHighest = new float[8];
    public static float[] AudioBand = new float[8];
    public static float[] AudioBandBuffer = new float[8];

    public static float Amplitude, AmplitudeBuffer;
    private float amplitudeHighest;

    // Use this for initialization
    void Start ()
	{
	    this.audioSource = this.GetComponent<AudioSource>();
    }

    void CreateAudioBands()
    {
        for (int i = 0; i < 8; i++)
        {
            if (this.freqBand[i] > this.freqBandHighest[i])
            {
                this.freqBandHighest[i] = this.freqBand[i];
            }
            AudioBand[i] = (this.freqBand[i] / this.freqBandHighest[i]);
            if (float.IsNaN(this.freqBandHighest[i]))
                AudioBandBuffer[i] = 0.0f;
            else
                AudioBandBuffer[i] = (this.bandBuffer[i] / this.freqBandHighest[i]);
        }
    }

    void Update()
    {
        this.GetSpectrumAudioSource();
        this.MakeFrequencyBands();
        this.BandBuffer();
        this.CreateAudioBands();
        this.GetAmplitude();
    }

    void GetAmplitude()
    {
        float currentAmplitude = 0;
        float currentAmplitudeBuffer = 0;
        for (int i = 0; i < 8; i++)
        {
            currentAmplitude += AudioBand[i];
            currentAmplitudeBuffer += AudioBandBuffer[i];
        }
        if (currentAmplitude > this.amplitudeHighest)
            this.amplitudeHighest = currentAmplitude;
        Amplitude = currentAmplitude / this.amplitudeHighest;
        AmplitudeBuffer = currentAmplitudeBuffer / this.amplitudeHighest;
    }

    private void GetSpectrumAudioSource()
    {
        this.audioSource.GetSpectrumData(this.SampleArr, 0, FFTWindow.Blackman);
    }

    private void MakeFrequencyBands()
    {
        int count = 0;
        for (int i = 0; i < 8; i++)
        {
            float average = 0;
            int sampleCount = (int)Mathf.Pow(2, i + 1);
            if (i == 7)
            {
                sampleCount += 2;
            }
            for (int j = 0; j < sampleCount; j++)
            {
                average += this.SampleArr[count] * (count + 1);
                count++;
            }
            average /= count;
            this.freqBand[i] = average * 10;
        }
    }

    void BandBuffer()
    {
        for (int i = 0; i < 8; i++)
        {
            if (this.freqBand[i] > this.bandBuffer[i])
            {
                this.bandBuffer[i] = this.freqBand[i];
                this.bufferDecrease[i] = 0.005f;
            }
            if (this.freqBand[i] < this.bandBuffer[i])
            {
                this.bandBuffer[i] -= this.bufferDecrease[i];
                this.bufferDecrease[i] *= 1.2f;
            }
        }
    }
}
